//
//  BVModifySexViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/10/21.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVModifySexViewController.h"
#import "BVUserSexCell.h"
#define GENDER_CELL_IDENTIFIER @"BVUserSexCell"

@interface BVModifySexViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSIndexPath *selectedIndex;
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation BVModifySexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"性别"];
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (NSIndexPath *)selectedIndex {
    if (!_selectedIndex) {
        NSInteger index;
        if (self.user.gender == 1) {
            index = 0;
        } else if (self.user.gender == 2) {
            index = 1;
        }
        _selectedIndex = [NSIndexPath indexPathForRow:index inSection:0];
    }
    return _selectedIndex;
}

#pragma mark -
#pragma mark - save
- (void)confirmSaveClick {
}

- (void)setTableView:(UITableView *)tableView {
    _tableView = tableView;
    [tableView registerNib:NIB_NAMED(GENDER_CELL_IDENTIFIER) forCellReuseIdentifier:GENDER_CELL_IDENTIFIER];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BVUserSexCell *gender_cell = [tableView dequeueReusableCellWithIdentifier:GENDER_CELL_IDENTIFIER forIndexPath:indexPath];
    gender_cell.sexTitle.text = self.titleArray[indexPath.row];
    
    // 默认选中第row行
    if (indexPath.row == self.selectedIndex.row) {
        [gender_cell show_select];
    }
    return gender_cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIndex = indexPath.row;
//    self.user.gender = self.selectIndex;
    if (self.selectIndex == 0) {
        self.selectIndex = 1;
    } else if (self.selectIndex == 1) {
        self.selectIndex = 2;
    }

    [self.hud show:YES];
    [JDStatusBarNotification showWithStatus:@"正在保存" styleName:JDStatusBarStyleDefault ];
    [self.mineTool modifyUserSex:self.selectIndex success:^{
        [self.hud hide:YES];
        [JDStatusBarNotification showWithStatus:@"保存成功" dismissAfter:2.0 styleName:JDStatusBarStyleSuccess];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [self.hud hide:YES];
        [self showError:@"保存失败"];
    }];
    
    [self setButtonIsSelectedWithIndexPath:indexPath];
}

- (void)setButtonIsSelectedWithIndexPath:(NSIndexPath *)indexPath{
    BVUserSexCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    // 如果选择的cell is_selected为no，显示btn，隐藏其他cell的btn
    if (!cell.is_selected) {
        for (int i = 0; i < self.titleArray.count; i++) {
            
            NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
            BVUserSexCell *sex_cell = [self.tableView cellForRowAtIndexPath:index];
            
            if (![cell isEqual:sex_cell]) { // 未选中的隐藏
                [sex_cell hide_select];
            }
            else {
                [cell show_select];
            }
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = @[@"男",@"女"];
    }
    return _titleArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
